<?php
namespace Admin\Controller;
use Common\Controller\AdminbaseController;

class ShopcatController extends AdminbaseController{

    protected $Shopcat_model;

    public function _initialize() {
        parent::_initialize();
        $this->Shopcat_model = D("Common/ShopCat");
    }

    // 分类列表
    public function index(){
        $parentid = I('request.parentid',0,'intval');
        if($parentid){
            $where['parentid'] = $parentid;
        }
        $catgs = $this->Shopcat_model->select();
        $cats=$this->Shopcat_model->where($where)->select();
        $this->assign("slidecats",$cats);
        $this->assign("catgs",$catgs);
        $this->assign("parentid",$parentid);
        $this->display();
    }

    // 分类添加
    public function add() {
        $parentid = I("get.parentid",0,'intval');
        $catgs = $this->Shopcat_model->where(array('cat_status'=>1))->select();
        $this->assign("catgs",$catgs);
        $this->display();
    }

    // 分类添加提交
    public function add_post() {
        if (IS_POST) {
            if ($this->Shopcat_model->create()!==false) {
                if ($this->Shopcat_model->add()!==false) {
                    $this->success("添加成功！", U("shopcat/index"));
                } else {
                    $this->error("添加失败！");
                }
            } else {
                $this->error($this->Shopcat_model->getError());
            }
        }
    }

    // 分类编辑
    public function edit(){
        $id= I("get.id",0,'intval');
        $slidecat=$this->Shopcat_model->where(array('cid'=>$id))->find();
        $catgs = $this->Shopcat_model->where(array('cat_status'=>1))->select();
        $this->assign("catgs",$catgs);
        $this->assign($slidecat);
        $this->display();
    }

    // 分类编辑提交
    public function edit_post(){
        if (IS_POST) {
            if ($this->Shopcat_model->create()!==false) {
                if ($this->Shopcat_model->save()!==false) {
                    $this->success("保存成功！", U("shopcat/index"));
                } else {
                    $this->error("保存失败！");
                }
            } else {
                $this->error($this->Shopcat_model->getError());
            }
        }
    }

    // 分类删除
    public function delete(){
        $id = I("get.id",0,'intval');
        if ($this->Shopcat_model->delete($id)!==false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }

    // 隐藏
    public function ban(){
        $id = I('get.id',0,'intval');
        if ($id) {
            $rst = $this->Shopcat_model->where(array('cid'=>$id))->save(array('cat_status'=>0));
            if ($rst) {
                $this->success("禁用成功！");
            } else {
                $this->error('禁用失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }

    // 启用
    public function cancelban(){
        $id = I('get.id',0,'intval');
        if ($id) {
            $result = $this->Shopcat_model->where(array('cid'=>$id))->save(array('cat_status'=>1));
            if ($result) {
                $this->success("启用成功！");
            } else {
                $this->error('启用失败！');
            }
        } else {
            $this->error('数据传入失败！');
        }
    }
}